package custom.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hichip.campro.R;
public class WarningDialogBuilder extends Dialog implements DialogInterface {


    private Effectstype type=null;
    private LinearLayout mLinearLayout;
    private View mDialogView;
    private TextView mTitle;
    private TextView mMessage;
    private int mDuration = -1;
    private static  int mOrientation=1;
    private boolean isCancelable=true;
    private volatile static WarningDialogBuilder instance;

    public WarningDialogBuilder(Context context) {
        super(context);
        init(context);

    }
    public WarningDialogBuilder(Context context,int theme) {
        super(context, theme);
        init(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = LayoutParams.MATCH_PARENT;
        params.width  = LayoutParams.MATCH_PARENT;
        getWindow().setAttributes((WindowManager.LayoutParams) params);

    }
    public static WarningDialogBuilder getInstance(Context context) {

        int ort=context.getResources().getConfiguration().orientation;
        if (mOrientation!=ort){
            mOrientation=ort;
            instance=null;
        }

        if (instance == null) {
            synchronized (NiftyDialogBuilder.class) {
                if (instance == null) {
                    instance = new WarningDialogBuilder(context, R.style.dialog_untran);
                }
            }
        }
        return instance;

    }

    private void init(Context context) {


        mDialogView = View.inflate(context, R.layout.dialog_warning, null);

        // mLinearLayoutView=(LinearLayout)mDialogView.findViewById(R.id.parentPanel);
        mLinearLayout=(LinearLayout)mDialogView.findViewById(R.id.warn_main);

        mTitle = (TextView) mDialogView.findViewById(R.id.dialog_title);
        mMessage = (TextView) mDialogView.findViewById(R.id.tv_content);

        setContentView(mDialogView);

        this.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {

                // mLinearLayoutView.setVisibility(View.VISIBLE);
                if(type==null){
                    type=Effectstype.Slidetop;
                }
                start(type);


            }
        });
        mLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isCancelable)dismiss();
            }
        });
    }


    public WarningDialogBuilder withTitle(CharSequence title) {
        mTitle.setText(title);
        return this;
    }

    public WarningDialogBuilder withTitleColor(String colorString) {
        mTitle.setTextColor(Color.parseColor(colorString));
        return this;
    }

    public WarningDialogBuilder withMessage(int textResId) {
        mMessage.setText(textResId);
        return this;
    }

    public WarningDialogBuilder withMessage(CharSequence msg) {
        mMessage.setText(msg);
        return this;
    }


    public WarningDialogBuilder withDuration(int duration) {
        this.mDuration=duration;
        return this;
    }

    public WarningDialogBuilder withEffect(Effectstype type) {
        this.type=type;
        return this;
    }

    public WarningDialogBuilder isCancelableOnTouchOutside(boolean cancelable) {
        this.isCancelable=cancelable;
        this.setCanceledOnTouchOutside(cancelable);
        return this;
    }

    public WarningDialogBuilder isCancelable(boolean cancelable) {
        this.isCancelable=cancelable;
        this.setCancelable(cancelable);
        return this;
    }

    @Override
    public void show() {

        super.show();
    }

    private void start(Effectstype type){
        BaseEffects animator = type.getAnimator();
        if(mDuration != -1){
            animator.setDuration(Math.abs(mDuration));
        }
        animator.start(mLinearLayout);
    }
    @Override
    public void dismiss() {
        super.dismiss();
        if(instance!=null){
            instance=null;
        }
    }
}
